package buco.integrity.osgi;

import java.util.*;
import java.util.Map.*;

import buco.core.api.*;

public class VersionIntegrity extends IntegrityCheck {

	public void checkIntegrity(Bundle bundle, IntegrityReport report) {
		List<BundleWire> wiresOut = this.composition.getOutgoingWires(bundle, false);
		if(wiresOut.size() < 2) 
			return;
		
		// Visitor wire metadata
		DuplicateExportVisitor visitor = new DuplicateExportVisitor(); 
		for(BundleWire wire : wiresOut) { 
			wire.accept(visitor); 
		}
		
		HashMap<String, List<Version>> exports = visitor.getExports(); 
		Iterator<Entry<String, List<Version>>> iterator = exports.entrySet().iterator(); 
		while(iterator.hasNext()) { 
			Entry<String, List<Version>> entry = iterator.next(); 
			String pck = entry.getKey(); 
			List<Version> versions = entry.getValue();
			if(versions != null && versions.size() > 1) { 
				// More than one candidate for an export! 
				report.warn(String.format("Different versions of package %s are imported.", pck));
			}			
		}
	}
}
